package com.coolw.dynamic.thread.pool.task;

import com.coolw.dynamic.thread.pool.context.DynamicThreadPoolManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicLong;

/**
 * demo task
 *
 * @author coolw
 * @date 2024/5/16 14:41
 */
@Slf4j
@Component
public class DemoTask {

    private static final AtomicLong COUNT_1 = new AtomicLong(0);
    private static final AtomicLong COUNT_2 = new AtomicLong(0);
    private static final AtomicLong COUNT_3 = new AtomicLong(0);
    private static final AtomicLong COUNT_4 = new AtomicLong(0);

    @PostConstruct
    public void init() {
        startTask();
    }

    private static void startTask() {
        try {
            new Thread(() -> startTask1()).start();
            new Thread(() -> startTask2()).start();
            new Thread(() -> startTask3()).start();
            new Thread(() -> startTask4()).start();
        } catch (Exception e) {
            log.error("startTask error", e);
        }
    }

    private static void startTask1() {
        String poolCode = "test1";
        ThreadPoolExecutor executor = DynamicThreadPoolManager.getExecutor(poolCode);
        if (executor == null) {
            log.info("线程池不存在,poolCode:{}", poolCode);
            return;
        }

        while (true) {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            executor.execute(() -> {
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                String threadName = Thread.currentThread().getName();
                long countVal = COUNT_1.incrementAndGet();
                log.info("当前线程池:[{}],线程名:[{}]已执行,COUNT_1:{}", poolCode, threadName, countVal);
            });
        }
    }

    private static void startTask2() {
        String poolCode = "test2";
        ThreadPoolExecutor executor = DynamicThreadPoolManager.getExecutor(poolCode);
        if (executor == null) {
            log.info("线程池不存在,poolCode:{}", poolCode);
            return;
        }

        while (true) {
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            executor.execute(() -> {
                try {
                    Thread.sleep(200L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                String threadName = Thread.currentThread().getName();
                long countVal = COUNT_2.incrementAndGet();
                log.info("当前线程池:[{}],线程名:[{}]已执行,COUNT_2:{}", poolCode, threadName, countVal);
            });
        }
    }

    private static void startTask3() {
        String poolCode = "test3";
        ThreadPoolExecutor executor = DynamicThreadPoolManager.getExecutor(poolCode);
        if (executor == null) {
            log.info("线程池不存在,poolCode:{}", poolCode);
            return;
        }

        while (true) {
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            executor.execute(() -> {
                try {
                    Thread.sleep(300L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                String threadName = Thread.currentThread().getName();
                long countVal = COUNT_3.incrementAndGet();
                log.info("当前线程池:[{}],线程名:[{}]已执行,COUNT_3:{}", poolCode, threadName, countVal);
            });
        }
    }

    private static void startTask4() {
        String poolCode = "test4";
        ThreadPoolExecutor executor = DynamicThreadPoolManager.getExecutor(poolCode);
        if (executor == null) {
            log.info("线程池不存在,poolCode:{}", poolCode);
            return;
        }

        while (true) {
            try {
                Thread.sleep(10L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            executor.execute(() -> {
                try {
                    Thread.sleep(180L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                String threadName = Thread.currentThread().getName();
                long countVal = COUNT_4.incrementAndGet();
                log.info("当前线程池:[{}],线程名:[{}]已执行,COUNT_4:{}", poolCode, threadName, countVal);
            });
        }
    }
}
